import threading, sys, pymysql,datetime
import baostock as bs
from queue import Queue
from dbManagement import DataManagement

class Multi_threading:

    def __init__(self):
        self.conn = DataManagement()
        # 连接数据库
        self.cursor = self.conn.connect_sql()
    #生成tablewidget点击后需要的参数表格
    def job_get_data_1(self,item2,item3):
        sql = '%' + item2 + '%'
        sql1 = "SELECT * FROM `kaipanla`.`stocklist` WHERE `date` LIKE '%s' AND `bai_kuai` LIKE '%s';" % (item3, sql)
        cc = self.cursor.execute(sql1)
        aa = []
        for i in range(cc):
            rowCount = self.cursor.fetchone()
            aa.append(rowCount[1])
        return aa



    #获取网络参数：板块对应盘子小的个股
    def job_get_data_get_net(self,item2,liu_tong_shi_zhi_1,q):

        #sql = '%' + item2 + '%'

        sql1 = "SELECT * FROM `kaipanla`.`kaipanla` WHERE  `ZSCode` = '%s' ;" % (item2)
        cc = self.cursor.execute(sql1)
        q1 = self.cursor.fetchone()
        q2 = q1[3]
        # 要传一个参数data给函数计算
        q3 = self.conn.ban_kuai_xuan_gu(12,q2)
        q4 = q3["Count"]

        q5 = self.conn.ban_kuai_xuan_gu(q4,q2)
        q6 = q5['list']
        for f in range(len(q6)):
            #未处理的市值参数
            print(q6[f][10])
            q7 = q6[f][10] / 100000000
            #处理好的参数
            liu_tong_shi_zhi = '%.2f' % (q7)
            if float(liu_tong_shi_zhi) <= 70.00:
                q.put(liu_tong_shi_zhi_1)


    # 整理20天日期并写入数组
    def jian_li_shu_zu(self):
        ri_qi_arry = []
        ri_qi_arry2 = []
        aa = 0;
        self.bb = 1;
        # 登录baostock服务器
        self.lg = bs.login()
        while self.bb <= 20:
            # 判断是否交易日
            self.ri_qi = (datetime.datetime.now() - datetime.timedelta(days=aa)).strftime("%Y-%m-%d")
            self.ri_qi2 = (datetime.datetime.now() - datetime.timedelta(days=aa)).strftime("%m-%d")
            self.guo_du_1 = bs.query_trade_dates(self.ri_qi)
            self.bb_1 = self.guo_du_1.get_row_data()[1]
            # 判断交易日，并放入指定数组self.ri_qi_arry
            if self.bb_1 == "1":
                #  建立数组
                ri_qi_arry.append(self.ri_qi)
                ri_qi_arry2.append(self.ri_qi2)
                self.bb += 1;
                aa += 1
                continue
            elif self.bb_1 == "0":
                aa += 1
                continue
            else:
                break

        return ri_qi_arry,ri_qi_arry2

    def huo_qu_zou_shi(self,code):
        #判断上海还是深圳股票
        if code[0:2] == "60":
            ri_xian = "http://image.sinajs.cn/newchart/daily/n/sh%s.gif" % (code)
            fen_shi = "http://image.sinajs.cn/newchart/min/n/sh%s.gif"% (code)

        else:
            ri_xian = "http://image.sinajs.cn/newchart/daily/n/sz%s.gif" % (code)
            fen_shi = "http://image.sinajs.cn/newchart/min/n/sz%s.gif" % (code)

        return ri_xian,fen_shi

    def hou_tai_ban_kuai_ku(self):
        pass




